package net.src.udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * 接收端
 */
public class ServerDemo {
    public static void main(String[] args) throws Exception {
        //创建接收端对象和端口
        DatagramSocket socket = new DatagramSocket(9999);

        //创建一个数据包接收数据
        byte[] buffer = new byte[1024*64];
        DatagramPacket packet = new DatagramPacket(buffer,buffer.length);

        System.out.println("客户端启动!");
        //不停接收数据
        while(true){
            socket.receive(packet);
            //获取发送端的ip和端口,获取对方端口,使用数据包的方法getlength获取接收的长度
            System.out.println("收到来自:"+packet.getAddress()+",端口为:"+packet.getPort()+"的消息:"+
                    new String(buffer,0, packet.getLength()));
        }

    }
}
